﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class Enemy : BaseObject
    {
        public bool m_bIsOnGround;
        public int m_nLevel;
        public int m_nHp;
        public int m_nStr;
        public int m_nInt;
        public int m_nDef;
        public int m_nAgi;
        public string m_szName;
        public string m_szClass;
        public string m_szElement;
        public string m_szSpecies;
        public Vector2 m_EnemySize;
        public Vector2 m_CollisionSize;

        public Enemy()
        {
            m_ObjectType = OBJ_TYPE.OBJ_ENEMY;
        }

        public void Load(string szFile)
        {
            FileStream fsFile = new FileStream(szFile, FileMode.Open);
            BinaryReader br = new BinaryReader(fsFile);
            m_szName = br.ReadString();
            m_Texture = GlobalContentManager.Instance.Texture("Enemies/" + Path.GetFileNameWithoutExtension(br.ReadString()));
            m_szClass = br.ReadString();
            m_szSpecies = br.ReadString();
            m_szElement = br.ReadString();

            m_EnemySize = new Vector2((int)br.ReadDecimal(), (int)br.ReadDecimal());
            m_CollisionSize = new Vector2((int)br.ReadDecimal(), (int)br.ReadDecimal());

            m_nLevel = (int)br.ReadDecimal();
            m_nHp = (int)br.ReadDecimal();
            m_nStr = (int)br.ReadDecimal();
            m_nDef = (int)br.ReadDecimal();
            m_nAgi = (int)br.ReadDecimal();
            m_nInt = (int)br.ReadDecimal();

            fsFile.Close();

        }

        public override void Update(float deltaT , KeyboardState LastKeyState) 
        {
            m_Position.Y += m_Velocity.Y * deltaT;
            m_Position.X += m_Velocity.X * deltaT;

            if (m_bIsOnGround == false)
                m_Velocity.Y += 6.81f;

            m_fFrame += deltaT * ((m_Velocity.X > 0.0f) ? m_Velocity.X : -m_Velocity.X) / 25.0f;
            if (m_fFrame >= m_nMaxFrame)
                m_fFrame = 0;

            m_bIsOnGround = false;
        }

        public override void Render(SpriteBatch spriteBatch, Camera cam)
        {
            spriteBatch.Draw(m_Texture, new Rectangle((int)(m_Position.X*cam.m_fZoom-cam.m_Position.X), (int)(m_Position.Y*cam.m_fZoom-cam.m_Position.Y), (int)(128*cam.m_fZoom), (int)(128*cam.m_fZoom)), new Rectangle(128 * (int)m_fFrame + (int)m_AnimOrigin.X, (int)m_AnimOrigin.Y, 128, 128), Color.White, 0.0f, new Vector2(64, 128), m_Direction, 0.0f);
        }

        public override void CollisionResponse(BaseObject other)
        {
            if (other.m_ObjectType == OBJ_TYPE.OBJ_PLAYER)
            {
                if (((Player)other).m_bAttacking == true && ((!((Player)other).m_bDucking && other.m_fFrame > 2) || (((Player)other).m_bDucking)))
                {
                    Rectangle weaponRect = new Rectangle((int)other.m_Position.X + (other.m_Direction == SpriteEffects.FlipHorizontally ? -84 : 0), (int)other.m_Position.Y - 128, 84, 100);
                    Rectangle enemyRect = new Rectangle((int)m_Position.X - 32, (int)m_Position.Y - 128, 64, 128);

                    if (weaponRect.Intersects(enemyRect))
                    {
                        m_Position.X += (m_Direction == SpriteEffects.FlipHorizontally ? 16 : -16);
                        if (other.m_Direction == SpriteEffects.FlipHorizontally)
                            m_Direction = SpriteEffects.None;
                        else
                            m_Direction = SpriteEffects.FlipHorizontally;
                    }
                }
            }
            else if (other.m_ObjectType == OBJ_TYPE.OBJ_TILE)
            {
                if (!LevelManager.Instance.m_TileProperties[((Tile)other).m_nTileID].m_bIsSolid)
                    return;

                Rectangle myFeet = new Rectangle((int)m_Position.X - 16, (int)m_Position.Y - 28, 32, 28);
                Rectangle myRect = new Rectangle((int)m_Position.X - 32, (int)m_Position.Y - 128, 64, 100);
                Rectangle tileRect = new Rectangle((int)other.m_Position.X, (int)other.m_Position.Y, (int)LevelManager.Instance.m_TileSize.X, (int)LevelManager.Instance.m_TileSize.Y);

                if (myFeet.Intersects(tileRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myFeet, tileRect);
                    if (m_Velocity.Y >= 0.0)
                    {
                        m_Position.Y -= (collRect.Height - 1) * Math.Sign(m_Position.Y - other.m_Position.Y);
                        m_Velocity.Y = 0.0f;
                        m_bIsOnGround = true;
                    }
                }
                else if (myRect.Intersects(tileRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myRect, tileRect);
                    Vector2 Angle = new Vector2(myRect.Center.Y - tileRect.Center.Y, myRect.X - tileRect.X);
                    if (collRect.Width > collRect.Height)
                    {
                        m_Position.Y += (collRect.Height + 1) * Math.Sign(m_Position.Y - other.m_Position.Y);
                        if (m_Velocity.Y < 0.0f)
                            m_Velocity.Y = 0.0f;
                    }
                    else
                        m_Position.X += (collRect.Width + 1) * Math.Sign(m_Position.X - other.m_Position.X);
                }
            }
        }
    }
}
